package Plugin_Reprise.editors.my_uvcmd_editor;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

public class EditorContentAssistProcessor implements IContentAssistProcessor {
	private WordProvider wordProvider; 
	private String lastError;
	
	EditorContentAssistProcessor() { 
		this.wordProvider = new WordProvider(); 
	} 

	
	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer textViewer,	int documentOffset) {
		IDocument document = textViewer.getDocument(); 
		int currOffset = documentOffset - 1;   
		
		
		
		
		try { 
			String currWord = ""; 
			char currChar; 
			while (currOffset > 0 
					&& !(Character.isWhitespace(currChar = document.getChar(currOffset)) 
					|| currChar == ';')) 
			{ 
				currWord = currChar + currWord; 
				currOffset--; 
				}   
			/*List 0) { */
				ICompletionProposal[] proposals = buildProposals(WordProvider.suggest(currWord), currWord, documentOffset - currWord.length()); 
				lastError = null; 
			/*	} */
			return proposals; 
			} 
		catch (Exception e) { 
			e.printStackTrace(); lastError = e.getMessage(); 
			} 
		return null; 
		
	}   
	
	private ICompletionProposal[] buildProposals(List< String > suggestions, String replacedWord, int offset) throws Exception { 
		ICompletionProposal[] proposals = new ICompletionProposal[suggestions.size()]; 
		int index = 0; 
		for (Iterator< String > i = suggestions.iterator(); i.hasNext();) { 
			String currSuggestion = (String) i.next(); 
			CompletionProposal cp = new CompletionProposal(currSuggestion, offset, replacedWord.length(), currSuggestion.length(), null, currSuggestion, null, null); 
			proposals[index] = cp; index++; 
			} 
		return proposals;
	}

	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer,
			int offset) {
		lastError = "No Context Information available"; 
		return null;
	}

	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getErrorMessage() {
		return lastError; 
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		// TODO Auto-generated method stub
		return null;
	}

}
